//
//  MineViewController.m
//  ACE
//
//  Created by Mickey Yin on 2021/12/1.
//

#import "MineViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

#import "AddressbookModel.h"
#import "MineTableViewCell.h"
#import "AddressbookViewController.h"
#import "IdentitycardViewController.h"
#import "LocationViewController.h"
#import "JXQRCodeViewController.h"
#import "YLJBrightnessViewController.h"
#import "WKWebViewController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (strong, nonatomic) CLLocation *location;
@property (nonatomic, strong) NSArray *mineList;

@end

@implementation MineViewController

//- (NSArray *)mineList {
//    if (!_mineList) {
//        _mineList = [NSArray new];
//    }
//    return _mineList;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.blueColor;
    self.navigationItem.title = @"我的";
    
    self.mineList = [NSArray arrayWithObjects:@"通讯录",@"身份证识别SDK",@"定位",@"二维码扫描",@"屏幕亮度调节",@"WKWebView使用（一）弹框实现", nil];
    
    [self setupUI];
}

- (void)setupUI {
    
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
//        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        _tableView.estimatedRowHeight = 100;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mineList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [MineTableViewCell cellWithTableView:tableView];
    cell.titleStr = self.mineList[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headvie = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 0)];
    return headvie;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[[AddressbookViewController alloc] init] animated:YES];
    }else if (indexPath.row == 1) {
        [self.navigationController pushViewController:[[IdentitycardViewController alloc] init] animated:YES];
    }else if (indexPath.row == 2) {
        [self.navigationController pushViewController:[[LocationViewController alloc] init] animated:YES];
    }else if (indexPath.row == 3) {
        [self.navigationController pushViewController:[[JXQRCodeViewController alloc] init] animated:YES];
    }else if (indexPath.row == 4) {
        [self.navigationController pushViewController:[[YLJBrightnessViewController alloc] init] animated:YES];
    }else {
        [self.navigationController pushViewController:[[WKWebViewController alloc] init] animated:YES];
    }
}


///iOS数组按中文关键字以字母序排序
//实现目标：数组中文字，按照首字母排序，并相同字放置一起（若首字母相同，查找第二个字母）
//大致思路：分两次实现，第一次按照字母排序，第二次判断首位汉字
+ (NSMutableArray *)paiduWithList:(NSArray *)list{

    NSMutableArray *people = [NSMutableArray arrayWithArray:list];
    for (int i = 0; i < people.count; ++i) {
        
        for (int j = 0; j < people.count-1; ++j) {
            AddressbookModel *model1 = people[j];
            AddressbookModel *model2 = people[j+1];
            
            if ([self isBiggerwithOne:model1 andTwo:model2]) {
                [people exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }

    NSMutableArray *arr = [self keywordPaixuList:people];
    
    return arr;
}

//2.上个步骤中使用的比较方法
+ (BOOL)isBiggerwithOne:(AddressbookModel *)model1 andTwo:(AddressbookModel *)model2 {

    NSString *name1;
    NSString *name2;
    if (model1.iphoneName.length > 0 && ![model1.iphoneName isEqualToString:@""] && ![model1.iphoneName isEqualToString:@"(null)"]) {
        name1 = model1.iphoneName;
    }else{
        name1 = model1.iphoneNumber;
    }

    if (model2.iphoneNumber.length > 0 && ![model2.iphoneNumber isEqualToString:@""] && ![model2.iphoneNumber isEqualToString:@"(null)"]) {
        name2 = model2.iphoneNumber;
    }else{
        name2 = model2.iphoneNumber;
    }

    //将取出的名字转换成字母
    NSMutableString *pinyin1 = [name1 mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformStripCombiningMarks, NO);

    //将拼音转换成大写拼音
    NSString * upPinyin1 = [pinyin1 uppercaseString];

    //取出第一个首字母当做字典的key
    NSString * firstStr1 = [upPinyin1 substringToIndex:1];
    NSMutableString *pinyin2 = [name2 mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformStripCombiningMarks, NO);

    //将拼音转换成大写拼音
    NSString * upPinyin2 = [pinyin2 uppercaseString];

    //取出第一个首字母当做字典的key
    NSString * firstStr2 = [upPinyin2 substringToIndex:1];
    if (firstStr1 > firstStr2) {
        return YES;
    }else if (firstStr1 < firstStr2){
        return NO;
    }else{
        //如果相等比较第二位
        if (upPinyin1.length > 1) {
            if (upPinyin2.length > 1) {
                //判断第二位
                NSString *secStr1 = [upPinyin1 substringWithRange:NSMakeRange(1, 1)];
                NSString *secStr2 = [upPinyin2 substringWithRange:NSMakeRange(1, 1)];
                if (secStr1 > secStr2) {
                    return YES;
                }else if (secStr1 < secStr2){
                    return NO;
                }else{
                    if (upPinyin1.length > 2) {
                        //判断第三位
                        if (upPinyin2.length > 2) {
                            NSString *treStr1 = [upPinyin1 substringWithRange:NSMakeRange(2, 1)];
                            NSString *treStr2 = [upPinyin2 substringWithRange:NSMakeRange(2, 1)];
                            if (treStr1 > treStr2) {
                                return YES;
                            }else if (treStr1 < treStr2) {
                                return NO;
                            }else{
                                if (upPinyin1.length > 3) {
                                    //判断第四位
                                    NSString *fourStr1 = [upPinyin1 substringWithRange:NSMakeRange(3, 1)];
                                    NSString *fourStr2 = [upPinyin2 substringWithRange:NSMakeRange(3, 1)];
                                    if (fourStr1 > fourStr2) {
                                        return YES;
                                    }else{
                                        return NO;
                                    }
                                }else{
                                    return NO;
                                }
                            }
                        }else{
                            return YES;
                        }
                    }else{
                        return NO;
                    }
                }
            }else{
                return YES;
            }
        }else{
            return NO;
        }
    }
}

//3.按照汉字第一位相同的，放置一起
+ (NSMutableArray *)keywordPaixuList:(NSArray *)list {
    
    NSMutableArray *people = [NSMutableArray arrayWithArray:list];
    int num = 0;
    for (int i = 0; i < people.count; ++i) {
        int creat = 1;
        for (int j = num+creat; j < people.count; j++) {
            AddressbookModel *model1 = people[i];
            AddressbookModel *model2 = people[j];
            NSString *name1;
            NSString *name2;

            if (model1.iphoneName.length > 0 && ![model1.iphoneName isEqualToString:@""] && ![model1.iphoneName isEqualToString:@"(null)"]) {
                name1 = model1.iphoneName;
            }else{
                name1 = model1.iphoneName;
            }
            if (model2.iphoneName.length > 0 && ![model2.iphoneName isEqualToString:@""] && ![model2.iphoneName isEqualToString:@"(null)"]) {
                name2 = model2.iphoneName;
            }else{
                name2 = model2.iphoneName;
            }

            if ([[name1 substringToIndex:1] isEqualToString:[name2 substringToIndex:1]]) {
                [people exchangeObjectAtIndex:num+creat withObjectAtIndex:j];
                creat++;
            }
        }
        
        num = num+creat;
        i = num-1;
    }
    
    return people;
}

- (NSArray *)jxbSort:(NSMutableArray *)jxbAry {
    NSArray *testArray = @[@"3", @"boarding", @"2", @"arrived", @"1"];
    NSArray *sortedArray = [testArray sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        NSInteger firstInteger = 0;
        BOOL isInteger1 = [[NSScanner scannerWithString:obj1] scanInteger:&firstInteger];
        
        NSInteger secondInteger = 0;
        BOOL isInteger2 = [[NSScanner scannerWithString:obj2] scanInteger:&secondInteger];

        if (isInteger1 && isInteger2) {
            if (firstInteger > secondInteger) {
                return NSOrderedDescending;
            }
            else if (secondInteger > firstInteger) {
                return NSOrderedAscending;
            }
            else {
                return NSOrderedSame;
            }
        }
        else if (isInteger1) {
            return NSOrderedDescending;
        }
        else if (isInteger2) {
            return NSOrderedAscending;
        }
        else {
            return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
        }
    }];
    return sortedArray;
}

@end
